from ..common.ui.PageElement import PageElement


class DatetimePicker(PageElement):

    def setValue(self, dateStr:str):
        """
        TODO 没有考虑组件startdate,enddate参数，如果超过范围应该抛错
        :param dateStr: 传入时间格式 如 "2014-09-15 13:30:21" 如果传入now，则填入当前时间
        :return: 
        """
        self._choose_date(dateStr)

    def _choose_date(self, date_str:str):
        self.wait_element_presence()
        if 'NOW' == date_str.upper():
            script = "$(arguments[0]).datetimepicker('value', new Date())"
        else:
            script = "$(arguments[0]).datetimepicker('value', '{value}')".format(value=date_str)

        self._execute_script(script, self._get_web_element(), date_str)

    def get_identification(self):
        identification = {'class': ['form-control'],
                          'data-ui-role': ['datetimepicker']}
        return identification
